package com.sinosoft.app.service.kafka;

import com.sinosoft.app.common.kafka.vo.*;
import com.sinosoft.app.dao.kafka.NewPostgreDao;
import com.sinosoft.app.dao.kafka.PostgreDao;
import com.sinosoft.app.utils.date.SimpleDateUtils;
import com.sinosoft.app.utils.kafka.DateUtil;
import com.sinosoft.app.utils.kafka.ParsingMsg2PostgresqlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Date;

@Service("postgreService")
public class PostgreService {

    public static final Logger log = LoggerFactory.getLogger(ParsingMsg2PostgresqlUtils.class);

    @Autowired
    private ParsingMsg2PostgresqlUtils parsingMsg2PostgresqlUtils;

    @Autowired
    private PostgreDao postgreDao;

    @Autowired
    private NewPostgreDao newPostgreDao;

    public NewEvent parseShemaMsg(String key, String value) { // 未作异常处理，后续处理
        return parsingMsg2PostgresqlUtils.parseShemaMsg(key, value);
    }

    public int postgresql2(NewEvent event, String table) {
        int count = 0;
        if (event.getValue().isEmpty()) {
            count = newPostgreDao.deleteById(event, table);
            log.info("deleteById ----:" + count);
            log.info("deleteById ----:" + event.getKey().get(0).getName());
        } else {
            count = newPostgreDao.upsert(event, table);
            log.info("save ----:" + count);
        }
        return count;
    }

    public void postgresql(Event event) throws Exception, SQLException {
        PrpDcompanyKafka company = new PrpDcompanyKafka();
        PrpDcompanyChild child = new PrpDcompanyChild();
        Class<? extends PrpDcompanyKafka> clazz = company.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (ValueField param : event.getValue()) {
            if (param.getValue() != null) {
                switch (param.getTypeclass()) {
                    case ParsingMsg2PostgresqlUtils.DatatypeWithoutName.TIMESTAMP6:
                        String interval = DateUtil.parseInterval((long) param.getValue());
                        param.setValue(interval);
                        break;
                    case ParsingMsg2PostgresqlUtils.DatatypeWithoutName.INTERVAL:
                        LocalDate localDate = DateUtil.parseDate((Long.parseLong(param.getValue().toString())));
                        param.setValue(localDate.toString());
                        break;
                    case ParsingMsg2PostgresqlUtils.DatatypeWithoutName.TIMESTAMP15:
                        LocalTime localTime = DateUtil.parseTime(Long.parseLong(param.getValue().toString()));
                        param.setValue(localTime.toString());
                        break;
                    case ParsingMsg2PostgresqlUtils.DatatypeWithoutName.TIMESTAMP16:
                        Date date = DateUtil.parseLongTime(Long.parseLong(param.getValue().toString()));
                        param.setValue(date);
                        break;
                }
            }
            if ("prpdcompany".equals(event.getTable()) && param.getValue() != null){
                for (Field field : fields) {
                    field.setAccessible(true);
                    if (param.getField().equals(field.getName().toLowerCase()))
                        if ("validDate,invalidDate,updateDate".contains(field.getName())){
                            Date date = SimpleDateUtils.parseUtilDate(param.getValue().toString());
                            field.set(company, date);
                        } else {
                            field.set(company, param.getValue());
                        }
                }
                if ("comcode".equals(param.getField())) {
                    child.setComCode(param.getValue().toString());
                } else if ("upperpath".equals(param.getField())) {
                    if (param.getValue() != null) {
                        child.setUpperPath(param.getValue().toString());
                        String[] split = param.getValue().toString().split(",");
                        for (int i = 0; i < split.length; i++) {
                            // 默认第一条为00000000, 不存, 从第二条开始传入child表
                            if (i == 1) {
                                child.setUpperComCode1(split[i]);
                            } else if (i == 2) {
                                child.setUpperComCode2(split[i]);
                            } else if (i == 3) {
                                child.setUpperComCode3(split[i]);
                            } else if (i == 4) {
                                child.setUpperComCode4(split[i]);
                            } else if (i == 5) {
                                child.setUpperComCode5(split[i]);
                            } else if (i == 6) {
                                child.setUpperComCode6(split[i]);
                            }
                        }
                    }
                }
            }
        }

        if ("prpdcompany".equals(event.getTable())){
            switch (event.getOp()) {
                case ParsingMsg2PostgresqlUtils.FULL:
                case ParsingMsg2PostgresqlUtils.INSERT:
                    postgreDao.insertPrpDCompany(company);
                    postgreDao.insertPrpDcompanyChild(child);
                    break;
                case ParsingMsg2PostgresqlUtils.UPDATE:
                    postgreDao.updatePrpDCompany(company);
                    postgreDao.updatePrpDcompanyChild(child);
                    break;
                case ParsingMsg2PostgresqlUtils.DELETE:
                    postgreDao.deletePrpDCompany(company);
                    postgreDao.deletePrpDcompanyChild(child);
                    break;
            }
        } else {
            switch (event.getOp()) {
                case ParsingMsg2PostgresqlUtils.FULL:
                case ParsingMsg2PostgresqlUtils.INSERT:
                    postgreDao.insert(event);
                    break;
                case ParsingMsg2PostgresqlUtils.UPDATE:
                    postgreDao.update(event);
                    break;
                case ParsingMsg2PostgresqlUtils.DELETE:
                    postgreDao.deleteById(event);
                    break;
            }
        }
    }
}
